<template>
  <div class="container">
    <div class="header">
      <el-input
        @keyup.enter.native="init"
        v-model="input"
        placeholder="请输入内容"
        clearable
      >
        <i
          slot="prefix"
          class="el-input__icon el-icon-search"
          @click="init"
        ></i>
      </el-input>
      <div class="right-group">
        <el-link type="danger" @click="batchDelete">批量删除</el-link>
        <el-divider direction="vertical"></el-divider>
        <el-link type="primary" @click="batchUpdateStatus(1)">批量启售</el-link>
        <el-divider direction="vertical"></el-divider>
        <el-link type="danger" @click="batchUpdateStatus(0)">批量停售</el-link>
        <el-button icon="el-icon-plus" class="btn" @click="add"
          >新增菜品</el-button
        >
      </div>
    </div>
    <el-table
      @selection-change="handleSelectionChange"
      :data="tableData"
      stripe
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="name" label="菜品名称"> </el-table-column>
      <el-table-column label="图片">
        <template v-slot="{ row }">
          <el-image
            style="width: 40px; height: 40px"
            :src="`${env}/common/download?name=${row.image}`"
            fit="cover"
            :preview-src-list="[`${env}/common/download?name=${row.image}`]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="菜品分类"> </el-table-column>
      <el-table-column prop="price" label="售价">
        <template v-slot="{ row }"> ¥{{ row.price / 100 }} </template>
      </el-table-column>
      <el-table-column prop="status" label="售卖状态">
        <template v-slot="{ row }">
          {{ row.status === 1 ? "启售" : "停售" }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="最后操作时间">
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="edit(row.id)">修改</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认更改该菜品状态?"
            @onConfirm="handleChangeStatus(row)"
          >
            <el-link slot="reference" type="primary">
              {{ row.status === 0 ? "启售" : "停售" }}
            </el-link>
          </el-popconfirm>
          <el-divider direction="vertical"></el-divider>
          <el-link type="danger" @click="deleteDish(row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getDishes,
  updateDish,
  deleteDish,
  batchDeleteDishes,
  batchUpdateDishStatus,
} from "@/api/dish";
export default {
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      input: "",
      tableData: [],
      multipleSelection: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
    };
  },
  created() {
    this.init();
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    init() {
      getDishes({ ...this.pageInfo, name: this.input }).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },

    handleChangeStatus(row) {
      updateDish({ id: row.id, status: +!row.status }).then((res) => {
        this.$message.success("菜品状态已经更改成功！");
        this.init();
      });
    },

    batchUpdateStatus(status) {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请至少选择一条记录");
        return;
      }
      const ids = this.multipleSelection.map((item) => item.id).join(",");
      batchUpdateDishStatus(ids, status).then((res) => {
        console.log("批量操作响应：", res);
        this.$message.success(status === 1 ? "批量启售成功" : "批量停售成功");
        this.init();
      });
    },
    deleteDish(id) {
      this.$confirm("确认删除该菜品吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteDish(id).then((res) => {
          this.$Message.success("删除成功");
          this.init();
        });
      });
    },
    batchDelete() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请至少选择一条记录");
        return;
      }
      const ids = this.multipleSelection.map((item) => item.id);
      this.$confirm("确认批量删除选中的菜品吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        batchDeleteDishes(ids.join(",")).then((res) => {
          this.$Message.success("批量删除成功");
          this.init();
        });
      });
    },

    add() {
      this.$router.push({ path: "/dish/add", query: { isEdit: "0" } });
    },
    edit(id) {
      this.$router.push({
        path: `/dish/change`,
        query: { isEdit: "1", id },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 30px;
  .header {
    display: flex;
    justify-content: space-between;
    .el-input {
      width: 250px;
      i {
        cursor: pointer;
      }
    }
    .right-group {
      display: flex;
      align-items: center;
      .btn {
        background-color: #ffc200;
        color: #333333;
      }
    }
  }
  .right-group .el-link,
  .right-group .el-button {
    margin-left: 20px;
    margin-right: 20px;
  }
  .el-table {
    margin-top: 20px;
  }
  .footer {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>
